# -*- coding: utf-8 -*-
# desc:遍历目录
import hashlib
import os

from src.palmer.server.scripts.case_sync_to_platform import Platform

BASE_PATH = "E:\workspace\gowork\src\easy-test"
SYNC_PATH = "E:\workspace\gowork\src\easy-test"


def get_md5(strs):
    m = hashlib.md5()
    m.update(strs.encode(encoding='utf-8'))
    return m.hexdigest()


def get_all_testcases():
    testcases = {}
    param = {
        "caseid": "2",
        "name": "2",
        "md5": "222",
        "father_md5": "1",
        "project_id": 1,
    }

    meau_param = {
        "name": "321-1",
        "md5": "5",
        "father_md5": "4",
        "project_id": 1,
    }
    for cur_dir, _, files in os.walk(SYNC_PATH):
        location = cur_dir.replace(BASE_PATH, "")
        if location.startswith("\\"):
            location = location[1:]
        location_list = location.split("\\")
        meau_param["father_md5"] = get_md5(location_list[-2]) if len(location_list) >= 2 else "-1"
        meau_param["name"] = location_list[-1]
        meau_param["md5"] = get_md5(meau_param["name"])
        plat.sync_testcase_meau(meau_param)

        for file in files:
            if file in ["__init__.py"]:
                continue
            if file.endswith(".py"):
                param["caseid"] = file.replace(".py", "")
                param["name"] = file.replace(".py", "")
                param["md5"] = get_md5(location + "@" + param["caseid"])
                param["father_md5"] = get_md5(location)

                plat.sync_testcase(param)


plat = Platform()
get_all_testcases()
